// package/pages/applyLeader/applyLeader.js
const utils = require('../../../utils/util.js');
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      name: "",
      group_name: "",
      phone: '请选择手机号码',
      phone_sign: ""
    },
    formData1: {
      name: "",
      group_name: "",
      phone: '请选择手机号码',
    },
    hiddenLoading: true,
    qrImg: '',
    pop: false
  },
  // 获取用户名
  getName(e) {
    const that = this;
    that.setData({
      [`formData.name`]: e.detail.value
    })
  },
  // 获取群名称
  getGroupName(e) {
    const that = this;
    that.setData({
      [`formData.group_name`]: e.detail.value
    })
  },
  // 获取手机号
  getPhoneNumber(e) {
    const that = this;
    console.log(e, 9999)
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let token = wx.getStorageSync('token');
      wx.login({
        success: function (res) {
          if (res.errMsg == "login:ok") {
            let data = {
              token,
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv,
              code: res.code
            }
            wx.showLoading({
              title: '加载中...',
            })
            myRequest.EasyHttp.post(api.reqUrls.getMiniPhoneNoBind, data).then(res => {
              wx.hideLoading();
              if (res.data.status == 1) {
                that.setData({
                  'formData.phone': res.data.result.mobile,
                  'formData.phone_sign': res.data.result.phone_sign
                })

              } else {
                wx.showModal({
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: '我知道了',
                  confirmColor: '#ff5151',
                  success(e) {}
                })
              }
            })
          }
        }
      })
    } else {
      return false;
    }

  },
  getRecode() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.qywxteamDetail, data).then(res => {
      if (res.data.status == 1) {
        that.setData({
          recommendStatus: true,
          [`formData.name`]: res.data.result.teamer.name,
          [`formData.group_name`]: res.data.result.teamer.group_name,
          [`formData.phone`]: res.data.result.teamer.phone,
          [`formData1.name`]: res.data.result.teamer.name,
          [`formData1.group_name`]: res.data.result.teamer.group_name,
          [`formData1.phone`]: res.data.result.teamer.phone,
        })
        that.getQrcode()
      } else {
        wx.hideLoading();
        that.setData({
          recommendStatus: false,
        })
      }
    })
  },
  getQrcode() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    myRequest.EasyHttp.post(api.reqUrls.qywxteamQrcode, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          qrImg: res.data.result.img
        })
      }
    })
  },
  // 编辑
  edit() {
    const that = this;
    that.setData({
      pop: true
    })
  },
  // 取消
  cancel() {
    const that = this;
    that.setData({
      pop: false
    })
  },
  Apply() {
    let that = this;
    wx.showModal({
      content: '是否提交申请团长?',
      success(prop) {
        if (prop.confirm) {
          if (!that.data.formData.phone_sign) {
            wx.showToast({
              title: '请选择手机号码',
              icon: 'none',
              duration: 1000
            })
            return;
          }
          if (!that.data.formData.name) {
            wx.showToast({
              title: '请填写用户名称',
              icon: 'none',
              duration: 1000
            })
            return;
          }
          if (!that.data.formData.group_name) {
            wx.showToast({
              title: '请填写群名称',
              icon: 'none',
              duration: 1000
            })
            return;
          }
          let data = {
            name: that.data.formData.name,
            token: wx.getStorageSync('token'),
            phone: that.data.formData.phone,
            phone_sign: that.data.formData.phone_sign,
            group_name: that.data.formData.group_name,
          }
          wx.showLoading({
            title: '加载中...',
          })
          myRequest.EasyHttp.post(api.reqUrls.qywxteamApply, data).then(res => {
            wx.hideLoading();
            if (res.data.status == 1) {
              that.setData({
                recommendStatus: res.data.status == 1 ? true : false,
              })
              that.getRecode()
            } else {
              wx.showModal({
                content: res.data.msg,
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#ff5151',
                success(e) {}
              })
            }
          })
          console.log('用户点击确定')
        } else if (prop.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  showImg() {
    const that = this;
    let imgList = []
    imgList.push(that.data.qrImg)
    wx.previewImage({
      current: 0, // 当前显示图片的http链接
      urls: imgList, // 需要预览的图片http链接列表
    })
  },
  // 编辑
  Applys() {
    let that = this;
    if (!that.data.formData.phone) {
      wx.showToast({
        title: '请选择手机号码',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!that.data.formData.name) {
      wx.showToast({
        title: '请填写用户名称',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!that.data.formData.group_name) {
      wx.showToast({
        title: '请填写群名称',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    let data = {
      name: that.data.formData.name,
      token: wx.getStorageSync('token'),
      phone: that.data.formData.phone,
      phone_sign: that.data.formData.phone_sign,
      group_name: that.data.formData.group_name,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.qywxteamUpdate, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {

        that.setData({
          recommendStatus: true,
          [`formData.name`]: res.data.result.teamer.name,
          [`formData.group_name`]: res.data.result.teamer.group_name,
          [`formData.phone`]: res.data.result.teamer.phone,
          [`formData1.name`]: res.data.result.teamer.name,
          [`formData1.group_name`]: res.data.result.teamer.group_name,
          [`formData1.phone`]: res.data.result.teamer.phone,
          pop: false
        })
        that.getQrcode()
      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {}
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      that.getRecode()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getRecode()
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})